/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
 *  Copyright (c) 2019-2024 QuestDB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

package io.questdb.test.cairo;

import io.questdb.test.AbstractCairoTest;
import org.junit.Test;

public class IndexReloadTest extends AbstractCairoTest {

    @Test
    public void testIndexRecreateAfterO3() throws Exception {
        // we need to find a good way to deal with this without having to remap the index
        assertMemoryLeak(() -> {
            execute("CREATE TABLE test_table (" +
                    "ts TIMESTAMP, " +
                    "code SYMBOL capacity 128, " +
                    "col1 DOUBLE, " +
                    "col2 DOUBLE, " +
                    "col3 DOUBLE, " +
                    "col4 DOUBLE, " +
                    "col5 DOUBLE" +
                    ") TIMESTAMP(ts) PARTITION BY HOUR WAL");

            execute("alter table test_table alter column code add index capacity 64");

            // Insert data spanning exactly 2 hours to create 2 partitions
            execute(
                    "INSERT INTO test_table " +
                            "SELECT " +
                            "timestamp_sequence('2023-01-01T00:00:00.000000Z'::timestamp, 1000000L) ts, " +
                            "rnd_symbol(100, 4, 10, 0) as code, " +
                            "rnd_double() * 100 as col1, " +
                            "rnd_double() * 1000 as col2, " +
                            "rnd_double() * 1000 as col3, " +
                            "rnd_double() * 1000 as col4, " +
                            "rnd_double() * 1000 as col5 " +
                            "FROM long_sequence(7200)"
            );

            drainWalQueue();

            // assert plan to ensure we're using index lookup
            assertSql(
                    "QUERY PLAN\n" +
                            "DeferredSingleSymbolFilterPageFrame\n" +
                            "    Index forward scan on: code\n" +
                            "      filter: code=53\n" +
                            "    Interval forward scan on: test_table\n" +
                            "      intervals: [(\"2023-01-01T01:00:00.000000Z\",\"2023-01-01T01:59:59.999999Z\")]\n",
                    "explain select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );

            String expectedBeforePartitionUpdate = "ts\tcode\tcol1\tcol2\tcol3\tcol4\tcol5\n" +
                    "2023-01-01T01:00:08.000000Z\tMLLEOYPHR\t83.30362051235157\t949.2600938169828\t867.2168415267288\t437.3382675977184\t961.9188181928822\n" +
                    "2023-01-01T01:00:11.000000Z\tMLLEOYPHR\t30.229726543073976\t388.8281917956169\t30.240051578341088\t740.1494495436722\t581.5818487587189\n" +
                    "2023-01-01T01:01:32.000000Z\tMLLEOYPHR\t45.91809297852849\t186.26833562073253\t65.34880403746135\t639.3702647053736\t136.19167923338748\n" +
                    "2023-01-01T01:03:47.000000Z\tMLLEOYPHR\t21.135989893165032\t103.97788782666117\t348.2339257512943\t780.3392717967885\t269.8354210284011\n" +
                    "2023-01-01T01:05:58.000000Z\tMLLEOYPHR\t11.639736953023816\t974.5809784741858\t237.67931890317274\t504.8726296391398\t688.7974901948697\n" +
                    "2023-01-01T01:08:05.000000Z\tMLLEOYPHR\t87.61426904688037\t883.8921097316043\t235.58791633461018\t121.75067783573668\t658.4184465864596\n" +
                    "2023-01-01T01:10:54.000000Z\tMLLEOYPHR\t54.64713601730965\t377.28569622737206\t761.0503541464711\t907.7738375575391\t197.9892218788082\n" +
                    "2023-01-01T01:12:59.000000Z\tMLLEOYPHR\t70.99254356432726\t403.5031647168245\t161.95548503926972\t142.07876532348175\t335.9923214829564\n" +
                    "2023-01-01T01:13:34.000000Z\tMLLEOYPHR\t38.157401050344106\t796.7306513189711\t879.3938838674152\t363.03538862111816\t41.542774341572716\n" +
                    "2023-01-01T01:14:07.000000Z\tMLLEOYPHR\t8.424116007022665\t376.06986696334377\t967.2991769108914\t399.30336805493073\t801.2525316611673\n" +
                    "2023-01-01T01:14:54.000000Z\tMLLEOYPHR\t88.21127710083458\t503.9067391920836\t294.4756043953227\t862.8942905469031\t813.4820106298038\n" +
                    "2023-01-01T01:16:13.000000Z\tMLLEOYPHR\t67.49238121449002\t167.0694293178011\t212.93088451440744\t836.2909487950873\t766.8012120687075\n" +
                    "2023-01-01T01:21:40.000000Z\tMLLEOYPHR\t51.940558328694664\t64.24691340417354\t120.08117564062093\t859.2001255211361\t435.4177882320567\n" +
                    "2023-01-01T01:25:14.000000Z\tMLLEOYPHR\t35.03491704992279\t164.6167179650041\t446.038815696435\t106.34729905874107\t998.7040510740878\n" +
                    "2023-01-01T01:25:33.000000Z\tMLLEOYPHR\t41.29690047157227\t167.2802892128822\t709.354893575967\t355.8353134274942\t388.31531461104083\n" +
                    "2023-01-01T01:26:12.000000Z\tMLLEOYPHR\t20.7000609304983\t153.52125437802943\t188.00969715105253\t28.262743456073046\t593.4594757241094\n" +
                    "2023-01-01T01:26:37.000000Z\tMLLEOYPHR\t31.736494908953173\t968.5297698518023\t887.6540885782375\t500.2474492393477\t658.4844831262895\n" +
                    "2023-01-01T01:27:09.000000Z\tMLLEOYPHR\t15.734598799973698\t783.1132298937488\t776.1283220830559\t724.3249889975006\t894.5840697515562\n" +
                    "2023-01-01T01:31:06.000000Z\tMLLEOYPHR\t87.9439085793801\t262.23547481415864\t24.39423504398386\t245.98085009934633\t20.41419692497659\n" +
                    "2023-01-01T01:31:19.000000Z\tMLLEOYPHR\t67.62067617469269\t492.4576154869693\t409.6802012275406\t291.57592054782964\t697.055620916249\n" +
                    "2023-01-01T01:33:27.000000Z\tMLLEOYPHR\t55.672353977659014\t488.56107227578474\t745.3677129759476\t137.64152489302063\t501.44346186886645\n" +
                    "2023-01-01T01:36:07.000000Z\tMLLEOYPHR\t61.57970642845324\t78.51375042424368\t661.3882399614453\t545.6054068161558\t405.37933267240555\n" +
                    "2023-01-01T01:37:37.000000Z\tMLLEOYPHR\t29.163288489495763\t731.8620711368229\t723.940243047161\t650.1618954997022\t64.3294386266443\n" +
                    "2023-01-01T01:40:53.000000Z\tMLLEOYPHR\t19.265556971285125\t16.4332950423004\t488.28004156324556\t811.3924665501961\t121.45316113479166\n" +
                    "2023-01-01T01:41:42.000000Z\tMLLEOYPHR\t74.27969678876866\t151.98165410094356\t609.4020182581351\t857.842350819325\t937.1274505656014\n" +
                    "2023-01-01T01:47:49.000000Z\tMLLEOYPHR\t93.01593955494064\t152.3751976444684\t513.9246605571283\t26.579517677078336\t897.1712144900602\n" +
                    "2023-01-01T01:49:01.000000Z\tMLLEOYPHR\t37.13678120897337\t548.0095430051164\t849.9171575436643\t151.37355866399426\t669.3670386387531\n" +
                    "2023-01-01T01:52:33.000000Z\tMLLEOYPHR\t12.540954187477737\t509.17581472140915\t231.45711075736207\t114.61430591822963\t103.93615669783873\n" +
                    "2023-01-01T01:52:44.000000Z\tMLLEOYPHR\t15.152026026269471\t321.18579021269625\t276.94589727215623\t751.9747498760804\t210.81698926796523\n" +
                    "2023-01-01T01:52:46.000000Z\tMLLEOYPHR\t95.9400284796692\t518.7892046097683\t676.3772272070913\t89.84867150317733\t881.7778862383221\n" +
                    "2023-01-01T01:53:22.000000Z\tMLLEOYPHR\t58.587789687856784\t636.3710871178623\t141.92237183067346\t151.09101570417926\t428.13165814803125\n" +
                    "2023-01-01T01:57:26.000000Z\tMLLEOYPHR\t44.97701503104481\t24.70585508780154\t269.1660652076486\t178.6033161217323\t965.4086375268596\n" +
                    "2023-01-01T01:57:31.000000Z\tMLLEOYPHR\t42.585755974021986\t914.4726474889443\t121.91724071033816\t673.2517353106563\t217.32644612662122\n" +
                    "2023-01-01T01:58:18.000000Z\tMLLEOYPHR\t64.36805129639127\t725.2094414205619\t484.53318622343534\t421.5387609207396\t305.2950322450768\n" +
                    "2023-01-01T01:59:03.000000Z\tMLLEOYPHR\t81.09705624996421\t568.1651364159121\t610.6658700259462\t797.1075978584797\t651.3974998850796\n" +
                    "2023-01-01T01:59:53.000000Z\tMLLEOYPHR\t67.75181589734471\t158.01628020506897\t8.233790039518073\t76.34223878707813\t727.7616998163442\n";

            String expectedAfterPartitionUpdate = "ts\tcode\tcol1\tcol2\tcol3\tcol4\tcol5\n" +
                    "2023-01-01T01:00:08.000000Z\tMLLEOYPHR\t83.30362051235157\t949.2600938169828\t867.2168415267288\t437.3382675977184\t961.9188181928822\n" +
                    "2023-01-01T01:00:11.000000Z\tMLLEOYPHR\t30.229726543073976\t388.8281917956169\t30.240051578341088\t740.1494495436722\t581.5818487587189\n" +
                    "2023-01-01T01:01:32.000000Z\tMLLEOYPHR\t45.91809297852849\t186.26833562073253\t65.34880403746135\t639.3702647053736\t136.19167923338748\n" +
                    "2023-01-01T01:03:47.000000Z\tMLLEOYPHR\t21.135989893165032\t103.97788782666117\t348.2339257512943\t780.3392717967885\t269.8354210284011\n" +
                    "2023-01-01T01:05:58.000000Z\tMLLEOYPHR\t11.639736953023816\t974.5809784741858\t237.67931890317274\t504.8726296391398\t688.7974901948697\n" +
                    "2023-01-01T01:08:05.000000Z\tMLLEOYPHR\t87.61426904688037\t883.8921097316043\t235.58791633461018\t121.75067783573668\t658.4184465864596\n" +
                    "2023-01-01T01:10:54.000000Z\tMLLEOYPHR\t54.64713601730965\t377.28569622737206\t761.0503541464711\t907.7738375575391\t197.9892218788082\n" +
                    "2023-01-01T01:12:59.000000Z\tMLLEOYPHR\t70.99254356432726\t403.5031647168245\t161.95548503926972\t142.07876532348175\t335.9923214829564\n" +
                    "2023-01-01T01:13:34.000000Z\tMLLEOYPHR\t38.157401050344106\t796.7306513189711\t879.3938838674152\t363.03538862111816\t41.542774341572716\n" +
                    "2023-01-01T01:14:07.000000Z\tMLLEOYPHR\t8.424116007022665\t376.06986696334377\t967.2991769108914\t399.30336805493073\t801.2525316611673\n" +
                    "2023-01-01T01:14:54.000000Z\tMLLEOYPHR\t88.21127710083458\t503.9067391920836\t294.4756043953227\t862.8942905469031\t813.4820106298038\n" +
                    "2023-01-01T01:16:13.000000Z\tMLLEOYPHR\t67.49238121449002\t167.0694293178011\t212.93088451440744\t836.2909487950873\t766.8012120687075\n" +
                    "2023-01-01T01:21:40.000000Z\tMLLEOYPHR\t51.940558328694664\t64.24691340417354\t120.08117564062093\t859.2001255211361\t435.4177882320567\n" +
                    "2023-01-01T01:25:14.000000Z\tMLLEOYPHR\t35.03491704992279\t164.6167179650041\t446.038815696435\t106.34729905874107\t998.7040510740878\n" +
                    "2023-01-01T01:25:33.000000Z\tMLLEOYPHR\t41.29690047157227\t167.2802892128822\t709.354893575967\t355.8353134274942\t388.31531461104083\n" +
                    "2023-01-01T01:26:12.000000Z\tMLLEOYPHR\t20.7000609304983\t153.52125437802943\t188.00969715105253\t28.262743456073046\t593.4594757241094\n" +
                    "2023-01-01T01:26:37.000000Z\tMLLEOYPHR\t31.736494908953173\t968.5297698518023\t887.6540885782375\t500.2474492393477\t658.4844831262895\n" +
                    "2023-01-01T01:27:09.000000Z\tMLLEOYPHR\t15.734598799973698\t783.1132298937488\t776.1283220830559\t724.3249889975006\t894.5840697515562\n" +
                    "2023-01-01T01:30:00.000000Z\tMLLEOYPHR\t100.0\t200.0\t300.0\t400.0\t500.0\n" +
                    "2023-01-01T01:31:06.000000Z\tMLLEOYPHR\t87.9439085793801\t262.23547481415864\t24.39423504398386\t245.98085009934633\t20.41419692497659\n" +
                    "2023-01-01T01:31:19.000000Z\tMLLEOYPHR\t67.62067617469269\t492.4576154869693\t409.6802012275406\t291.57592054782964\t697.055620916249\n" +
                    "2023-01-01T01:33:27.000000Z\tMLLEOYPHR\t55.672353977659014\t488.56107227578474\t745.3677129759476\t137.64152489302063\t501.44346186886645\n" +
                    "2023-01-01T01:36:07.000000Z\tMLLEOYPHR\t61.57970642845324\t78.51375042424368\t661.3882399614453\t545.6054068161558\t405.37933267240555\n" +
                    "2023-01-01T01:37:37.000000Z\tMLLEOYPHR\t29.163288489495763\t731.8620711368229\t723.940243047161\t650.1618954997022\t64.3294386266443\n" +
                    "2023-01-01T01:40:53.000000Z\tMLLEOYPHR\t19.265556971285125\t16.4332950423004\t488.28004156324556\t811.3924665501961\t121.45316113479166\n" +
                    "2023-01-01T01:41:42.000000Z\tMLLEOYPHR\t74.27969678876866\t151.98165410094356\t609.4020182581351\t857.842350819325\t937.1274505656014\n" +
                    "2023-01-01T01:45:00.000000Z\tMLLEOYPHR\t150.0\t250.0\t350.0\t450.0\t550.0\n" +
                    "2023-01-01T01:47:49.000000Z\tMLLEOYPHR\t93.01593955494064\t152.3751976444684\t513.9246605571283\t26.579517677078336\t897.1712144900602\n" +
                    "2023-01-01T01:49:01.000000Z\tMLLEOYPHR\t37.13678120897337\t548.0095430051164\t849.9171575436643\t151.37355866399426\t669.3670386387531\n" +
                    "2023-01-01T01:52:33.000000Z\tMLLEOYPHR\t12.540954187477737\t509.17581472140915\t231.45711075736207\t114.61430591822963\t103.93615669783873\n" +
                    "2023-01-01T01:52:44.000000Z\tMLLEOYPHR\t15.152026026269471\t321.18579021269625\t276.94589727215623\t751.9747498760804\t210.81698926796523\n" +
                    "2023-01-01T01:52:46.000000Z\tMLLEOYPHR\t95.9400284796692\t518.7892046097683\t676.3772272070913\t89.84867150317733\t881.7778862383221\n" +
                    "2023-01-01T01:53:22.000000Z\tMLLEOYPHR\t58.587789687856784\t636.3710871178623\t141.92237183067346\t151.09101570417926\t428.13165814803125\n" +
                    "2023-01-01T01:57:26.000000Z\tMLLEOYPHR\t44.97701503104481\t24.70585508780154\t269.1660652076486\t178.6033161217323\t965.4086375268596\n" +
                    "2023-01-01T01:57:31.000000Z\tMLLEOYPHR\t42.585755974021986\t914.4726474889443\t121.91724071033816\t673.2517353106563\t217.32644612662122\n" +
                    "2023-01-01T01:58:18.000000Z\tMLLEOYPHR\t64.36805129639127\t725.2094414205619\t484.53318622343534\t421.5387609207396\t305.2950322450768\n" +
                    "2023-01-01T01:59:03.000000Z\tMLLEOYPHR\t81.09705624996421\t568.1651364159121\t610.6658700259462\t797.1075978584797\t651.3974998850796\n" +
                    "2023-01-01T01:59:53.000000Z\tMLLEOYPHR\t67.75181589734471\t158.01628020506897\t8.233790039518073\t76.34223878707813\t727.7616998163442\n";

            assertSql(
                    expectedBeforePartitionUpdate,
                    "select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );

            // Insert 2 more rows with code 'MLLEOYPHR' in the second partition, O3, non-append
            execute("INSERT INTO test_table VALUES " +
                    "('2023-01-01T01:30:00.000000Z', 'MLLEOYPHR', 100.0, 200.0, 300.0, 400.0, 500.0), " +
                    "('2023-01-01T01:45:00.000000Z', 'MLLEOYPHR', 150.0, 250.0, 350.0, 450.0, 550.0)");

            drainWalQueue();

            // is this a cached TableReader?
            assertSql(
                    expectedAfterPartitionUpdate,
                    "select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );
        });
    }

    @Test
    public void testIndexReloadAfterAppend() throws Exception {
        // we need to find a good way to deal with this without having to remap the index
        assertMemoryLeak(() -> {
            execute("CREATE TABLE test_table (" +
                    "ts TIMESTAMP, " +
                    "code SYMBOL capacity 128, " +
                    "col1 DOUBLE, " +
                    "col2 DOUBLE, " +
                    "col3 DOUBLE, " +
                    "col4 DOUBLE, " +
                    "col5 DOUBLE" +
                    ") TIMESTAMP(ts) PARTITION BY HOUR WAL");

            execute("alter table test_table alter column code add index capacity 64");

            // Insert data spanning exactly 2 hours to create 2 partitions
            execute(
                    "INSERT INTO test_table " +
                            "SELECT " +
                            "timestamp_sequence('2023-01-01T00:00:00.000000Z'::timestamp, 1000000L) ts, " +
                            "rnd_symbol(100, 4, 10, 0) as code, " +
                            "rnd_double() * 100 as col1, " +
                            "rnd_double() * 1000 as col2, " +
                            "rnd_double() * 1000 as col3, " +
                            "rnd_double() * 1000 as col4, " +
                            "rnd_double() * 1000 as col5 " +
                            "FROM long_sequence(7200)"
            );

            drainWalQueue();

            // assert plan to ensure we're using index lookup
            assertSql(
                    "QUERY PLAN\n" +
                            "DeferredSingleSymbolFilterPageFrame\n" +
                            "    Index forward scan on: code\n" +
                            "      filter: code=53\n" +
                            "    Interval forward scan on: test_table\n" +
                            "      intervals: [(\"2023-01-01T01:00:00.000000Z\",\"2023-01-01T01:59:59.999999Z\")]\n",
                    "explain select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );

            String expectedBeforePartitionUpdate = "ts\tcode\tcol1\tcol2\tcol3\tcol4\tcol5\n" +
                    "2023-01-01T01:00:08.000000Z\tMLLEOYPHR\t83.30362051235157\t949.2600938169828\t867.2168415267288\t437.3382675977184\t961.9188181928822\n" +
                    "2023-01-01T01:00:11.000000Z\tMLLEOYPHR\t30.229726543073976\t388.8281917956169\t30.240051578341088\t740.1494495436722\t581.5818487587189\n" +
                    "2023-01-01T01:01:32.000000Z\tMLLEOYPHR\t45.91809297852849\t186.26833562073253\t65.34880403746135\t639.3702647053736\t136.19167923338748\n" +
                    "2023-01-01T01:03:47.000000Z\tMLLEOYPHR\t21.135989893165032\t103.97788782666117\t348.2339257512943\t780.3392717967885\t269.8354210284011\n" +
                    "2023-01-01T01:05:58.000000Z\tMLLEOYPHR\t11.639736953023816\t974.5809784741858\t237.67931890317274\t504.8726296391398\t688.7974901948697\n" +
                    "2023-01-01T01:08:05.000000Z\tMLLEOYPHR\t87.61426904688037\t883.8921097316043\t235.58791633461018\t121.75067783573668\t658.4184465864596\n" +
                    "2023-01-01T01:10:54.000000Z\tMLLEOYPHR\t54.64713601730965\t377.28569622737206\t761.0503541464711\t907.7738375575391\t197.9892218788082\n" +
                    "2023-01-01T01:12:59.000000Z\tMLLEOYPHR\t70.99254356432726\t403.5031647168245\t161.95548503926972\t142.07876532348175\t335.9923214829564\n" +
                    "2023-01-01T01:13:34.000000Z\tMLLEOYPHR\t38.157401050344106\t796.7306513189711\t879.3938838674152\t363.03538862111816\t41.542774341572716\n" +
                    "2023-01-01T01:14:07.000000Z\tMLLEOYPHR\t8.424116007022665\t376.06986696334377\t967.2991769108914\t399.30336805493073\t801.2525316611673\n" +
                    "2023-01-01T01:14:54.000000Z\tMLLEOYPHR\t88.21127710083458\t503.9067391920836\t294.4756043953227\t862.8942905469031\t813.4820106298038\n" +
                    "2023-01-01T01:16:13.000000Z\tMLLEOYPHR\t67.49238121449002\t167.0694293178011\t212.93088451440744\t836.2909487950873\t766.8012120687075\n" +
                    "2023-01-01T01:21:40.000000Z\tMLLEOYPHR\t51.940558328694664\t64.24691340417354\t120.08117564062093\t859.2001255211361\t435.4177882320567\n" +
                    "2023-01-01T01:25:14.000000Z\tMLLEOYPHR\t35.03491704992279\t164.6167179650041\t446.038815696435\t106.34729905874107\t998.7040510740878\n" +
                    "2023-01-01T01:25:33.000000Z\tMLLEOYPHR\t41.29690047157227\t167.2802892128822\t709.354893575967\t355.8353134274942\t388.31531461104083\n" +
                    "2023-01-01T01:26:12.000000Z\tMLLEOYPHR\t20.7000609304983\t153.52125437802943\t188.00969715105253\t28.262743456073046\t593.4594757241094\n" +
                    "2023-01-01T01:26:37.000000Z\tMLLEOYPHR\t31.736494908953173\t968.5297698518023\t887.6540885782375\t500.2474492393477\t658.4844831262895\n" +
                    "2023-01-01T01:27:09.000000Z\tMLLEOYPHR\t15.734598799973698\t783.1132298937488\t776.1283220830559\t724.3249889975006\t894.5840697515562\n" +
                    "2023-01-01T01:31:06.000000Z\tMLLEOYPHR\t87.9439085793801\t262.23547481415864\t24.39423504398386\t245.98085009934633\t20.41419692497659\n" +
                    "2023-01-01T01:31:19.000000Z\tMLLEOYPHR\t67.62067617469269\t492.4576154869693\t409.6802012275406\t291.57592054782964\t697.055620916249\n" +
                    "2023-01-01T01:33:27.000000Z\tMLLEOYPHR\t55.672353977659014\t488.56107227578474\t745.3677129759476\t137.64152489302063\t501.44346186886645\n" +
                    "2023-01-01T01:36:07.000000Z\tMLLEOYPHR\t61.57970642845324\t78.51375042424368\t661.3882399614453\t545.6054068161558\t405.37933267240555\n" +
                    "2023-01-01T01:37:37.000000Z\tMLLEOYPHR\t29.163288489495763\t731.8620711368229\t723.940243047161\t650.1618954997022\t64.3294386266443\n" +
                    "2023-01-01T01:40:53.000000Z\tMLLEOYPHR\t19.265556971285125\t16.4332950423004\t488.28004156324556\t811.3924665501961\t121.45316113479166\n" +
                    "2023-01-01T01:41:42.000000Z\tMLLEOYPHR\t74.27969678876866\t151.98165410094356\t609.4020182581351\t857.842350819325\t937.1274505656014\n" +
                    "2023-01-01T01:47:49.000000Z\tMLLEOYPHR\t93.01593955494064\t152.3751976444684\t513.9246605571283\t26.579517677078336\t897.1712144900602\n" +
                    "2023-01-01T01:49:01.000000Z\tMLLEOYPHR\t37.13678120897337\t548.0095430051164\t849.9171575436643\t151.37355866399426\t669.3670386387531\n" +
                    "2023-01-01T01:52:33.000000Z\tMLLEOYPHR\t12.540954187477737\t509.17581472140915\t231.45711075736207\t114.61430591822963\t103.93615669783873\n" +
                    "2023-01-01T01:52:44.000000Z\tMLLEOYPHR\t15.152026026269471\t321.18579021269625\t276.94589727215623\t751.9747498760804\t210.81698926796523\n" +
                    "2023-01-01T01:52:46.000000Z\tMLLEOYPHR\t95.9400284796692\t518.7892046097683\t676.3772272070913\t89.84867150317733\t881.7778862383221\n" +
                    "2023-01-01T01:53:22.000000Z\tMLLEOYPHR\t58.587789687856784\t636.3710871178623\t141.92237183067346\t151.09101570417926\t428.13165814803125\n" +
                    "2023-01-01T01:57:26.000000Z\tMLLEOYPHR\t44.97701503104481\t24.70585508780154\t269.1660652076486\t178.6033161217323\t965.4086375268596\n" +
                    "2023-01-01T01:57:31.000000Z\tMLLEOYPHR\t42.585755974021986\t914.4726474889443\t121.91724071033816\t673.2517353106563\t217.32644612662122\n" +
                    "2023-01-01T01:58:18.000000Z\tMLLEOYPHR\t64.36805129639127\t725.2094414205619\t484.53318622343534\t421.5387609207396\t305.2950322450768\n" +
                    "2023-01-01T01:59:03.000000Z\tMLLEOYPHR\t81.09705624996421\t568.1651364159121\t610.6658700259462\t797.1075978584797\t651.3974998850796\n" +
                    "2023-01-01T01:59:53.000000Z\tMLLEOYPHR\t67.75181589734471\t158.01628020506897\t8.233790039518073\t76.34223878707813\t727.7616998163442\n";

            String expectedAfterPartitionUpdate = "ts\tcode\tcol1\tcol2\tcol3\tcol4\tcol5\n" +
                    "2023-01-01T01:00:08.000000Z\tMLLEOYPHR\t83.30362051235157\t949.2600938169828\t867.2168415267288\t437.3382675977184\t961.9188181928822\n" +
                    "2023-01-01T01:00:11.000000Z\tMLLEOYPHR\t30.229726543073976\t388.8281917956169\t30.240051578341088\t740.1494495436722\t581.5818487587189\n" +
                    "2023-01-01T01:01:32.000000Z\tMLLEOYPHR\t45.91809297852849\t186.26833562073253\t65.34880403746135\t639.3702647053736\t136.19167923338748\n" +
                    "2023-01-01T01:03:47.000000Z\tMLLEOYPHR\t21.135989893165032\t103.97788782666117\t348.2339257512943\t780.3392717967885\t269.8354210284011\n" +
                    "2023-01-01T01:05:58.000000Z\tMLLEOYPHR\t11.639736953023816\t974.5809784741858\t237.67931890317274\t504.8726296391398\t688.7974901948697\n" +
                    "2023-01-01T01:08:05.000000Z\tMLLEOYPHR\t87.61426904688037\t883.8921097316043\t235.58791633461018\t121.75067783573668\t658.4184465864596\n" +
                    "2023-01-01T01:10:54.000000Z\tMLLEOYPHR\t54.64713601730965\t377.28569622737206\t761.0503541464711\t907.7738375575391\t197.9892218788082\n" +
                    "2023-01-01T01:12:59.000000Z\tMLLEOYPHR\t70.99254356432726\t403.5031647168245\t161.95548503926972\t142.07876532348175\t335.9923214829564\n" +
                    "2023-01-01T01:13:34.000000Z\tMLLEOYPHR\t38.157401050344106\t796.7306513189711\t879.3938838674152\t363.03538862111816\t41.542774341572716\n" +
                    "2023-01-01T01:14:07.000000Z\tMLLEOYPHR\t8.424116007022665\t376.06986696334377\t967.2991769108914\t399.30336805493073\t801.2525316611673\n" +
                    "2023-01-01T01:14:54.000000Z\tMLLEOYPHR\t88.21127710083458\t503.9067391920836\t294.4756043953227\t862.8942905469031\t813.4820106298038\n" +
                    "2023-01-01T01:16:13.000000Z\tMLLEOYPHR\t67.49238121449002\t167.0694293178011\t212.93088451440744\t836.2909487950873\t766.8012120687075\n" +
                    "2023-01-01T01:21:40.000000Z\tMLLEOYPHR\t51.940558328694664\t64.24691340417354\t120.08117564062093\t859.2001255211361\t435.4177882320567\n" +
                    "2023-01-01T01:25:14.000000Z\tMLLEOYPHR\t35.03491704992279\t164.6167179650041\t446.038815696435\t106.34729905874107\t998.7040510740878\n" +
                    "2023-01-01T01:25:33.000000Z\tMLLEOYPHR\t41.29690047157227\t167.2802892128822\t709.354893575967\t355.8353134274942\t388.31531461104083\n" +
                    "2023-01-01T01:26:12.000000Z\tMLLEOYPHR\t20.7000609304983\t153.52125437802943\t188.00969715105253\t28.262743456073046\t593.4594757241094\n" +
                    "2023-01-01T01:26:37.000000Z\tMLLEOYPHR\t31.736494908953173\t968.5297698518023\t887.6540885782375\t500.2474492393477\t658.4844831262895\n" +
                    "2023-01-01T01:27:09.000000Z\tMLLEOYPHR\t15.734598799973698\t783.1132298937488\t776.1283220830559\t724.3249889975006\t894.5840697515562\n" +
                    "2023-01-01T01:31:06.000000Z\tMLLEOYPHR\t87.9439085793801\t262.23547481415864\t24.39423504398386\t245.98085009934633\t20.41419692497659\n" +
                    "2023-01-01T01:31:19.000000Z\tMLLEOYPHR\t67.62067617469269\t492.4576154869693\t409.6802012275406\t291.57592054782964\t697.055620916249\n" +
                    "2023-01-01T01:33:27.000000Z\tMLLEOYPHR\t55.672353977659014\t488.56107227578474\t745.3677129759476\t137.64152489302063\t501.44346186886645\n" +
                    "2023-01-01T01:36:07.000000Z\tMLLEOYPHR\t61.57970642845324\t78.51375042424368\t661.3882399614453\t545.6054068161558\t405.37933267240555\n" +
                    "2023-01-01T01:37:37.000000Z\tMLLEOYPHR\t29.163288489495763\t731.8620711368229\t723.940243047161\t650.1618954997022\t64.3294386266443\n" +
                    "2023-01-01T01:40:53.000000Z\tMLLEOYPHR\t19.265556971285125\t16.4332950423004\t488.28004156324556\t811.3924665501961\t121.45316113479166\n" +
                    "2023-01-01T01:41:42.000000Z\tMLLEOYPHR\t74.27969678876866\t151.98165410094356\t609.4020182581351\t857.842350819325\t937.1274505656014\n" +
                    "2023-01-01T01:47:49.000000Z\tMLLEOYPHR\t93.01593955494064\t152.3751976444684\t513.9246605571283\t26.579517677078336\t897.1712144900602\n" +
                    "2023-01-01T01:49:01.000000Z\tMLLEOYPHR\t37.13678120897337\t548.0095430051164\t849.9171575436643\t151.37355866399426\t669.3670386387531\n" +
                    "2023-01-01T01:52:33.000000Z\tMLLEOYPHR\t12.540954187477737\t509.17581472140915\t231.45711075736207\t114.61430591822963\t103.93615669783873\n" +
                    "2023-01-01T01:52:44.000000Z\tMLLEOYPHR\t15.152026026269471\t321.18579021269625\t276.94589727215623\t751.9747498760804\t210.81698926796523\n" +
                    "2023-01-01T01:52:46.000000Z\tMLLEOYPHR\t95.9400284796692\t518.7892046097683\t676.3772272070913\t89.84867150317733\t881.7778862383221\n" +
                    "2023-01-01T01:53:22.000000Z\tMLLEOYPHR\t58.587789687856784\t636.3710871178623\t141.92237183067346\t151.09101570417926\t428.13165814803125\n" +
                    "2023-01-01T01:57:26.000000Z\tMLLEOYPHR\t44.97701503104481\t24.70585508780154\t269.1660652076486\t178.6033161217323\t965.4086375268596\n" +
                    "2023-01-01T01:57:31.000000Z\tMLLEOYPHR\t42.585755974021986\t914.4726474889443\t121.91724071033816\t673.2517353106563\t217.32644612662122\n" +
                    "2023-01-01T01:58:18.000000Z\tMLLEOYPHR\t64.36805129639127\t725.2094414205619\t484.53318622343534\t421.5387609207396\t305.2950322450768\n" +
                    "2023-01-01T01:59:03.000000Z\tMLLEOYPHR\t81.09705624996421\t568.1651364159121\t610.6658700259462\t797.1075978584797\t651.3974998850796\n" +
                    "2023-01-01T01:59:53.000000Z\tMLLEOYPHR\t67.75181589734471\t158.01628020506897\t8.233790039518073\t76.34223878707813\t727.7616998163442\n" +
                    "2023-01-01T01:59:59.000000Z\tMLLEOYPHR\t100.0\t200.0\t300.0\t400.0\t500.0\n" +
                    "2023-01-01T01:59:59.000000Z\tMLLEOYPHR\t150.0\t250.0\t350.0\t450.0\t550.0\n";

            assertSql(
                    expectedBeforePartitionUpdate,
                    "select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );

            // Insert 2 more rows with code 'MLLEOYPHR' in the second partition, O3, non-append
            execute("INSERT INTO test_table VALUES " +
                    "('2023-01-01T01:59:59.000000Z', 'MLLEOYPHR', 100.0, 200.0, 300.0, 400.0, 500.0), " +
                    "('2023-01-01T01:59:59.000000Z', 'MLLEOYPHR', 150.0, 250.0, 350.0, 450.0, 550.0)");

            drainWalQueue();

            // is this a cached TableReader?
            assertSql(
                    expectedAfterPartitionUpdate,
                    "select * from test_table where code = 'MLLEOYPHR' and ts in '2023-01-01T01'"
            );
        });
    }
}
